package collectionDemo;

import org.apache.log4j.Logger;
import org.junit.Test;
import utils.PrintUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;

/**
 * 一维的可以。
 * 二维数组不能 clone 。复制的引用。
 */
public class ArrayListDemo {
	private static final Logger logger = Logger.getLogger(ArrayListDemo.class);

	public static void main(String[] args) {

		ArrayList<Object> objects = new ArrayList<>(1000);
		System.out.println("ArrayListDemo:main:init initialCapacity 1000, list size:"+objects.size()+", isEmpty:"+objects.isEmpty());
//		objects.add(2,3);

		int [] origin = {1};
		int [][] origin2 = {{1},{2}};

		int[] clone = origin.clone();
		clone[0] = 0;
		System.out.println("ArrayListDemo:main:origin:"+origin[0]);//1 origin 没改变

		int[][] clone2 = origin2.clone();
		clone2[0][0] = 0;
		clone2[1][0] = 3;
		System.out.println("ArrayListDemo:main:origin2::"+origin2[0][0]);//0，origin 已改变
		System.out.println("ArrayListDemo:main:origin2::"+origin2[1][0]);//3，origin 已改变

		// inside();

		//iteratorList();
//		addSecondLastPosition();
		addCollection();


		// addSecondLastPosition();
		// addCollection();
		// iteratorList();
		replaceElement();

	}

	@Test
	public void delElement(){
		logger.info("delElement: ");
		List<Integer> list = new ArrayList<>();
		for (int i = 0; i < 5; i++) {
			list.add(i);
		}


		Iterator<Integer> iterator = list.iterator();
		while (iterator.hasNext() && list.size()>1){
			Integer next = iterator.next();//需要加这一行才能删除否则，，，出错。  java.lang.IllegalStateException   >>> next() :lastRet = i
			iterator.remove();
		}

		PrintUtil.print(list);
	}

	/**
	 * result: can't set .when (set index > size-1.)
	 * set(index,obj) java.lang.IndexOutOfBoundsException:
	 */
	@Test
	public void arraylistSetVsAdd(){
		List<String> arrayList = new ArrayList<>();
		arrayList.set(1,"1");//java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
		PrintUtil.print(arrayList);

	}

	// 替代元素
	private static void replaceElement() {
		List<Man> oList = new ArrayList<>();
		System.out.println(oList.size() + " size ");
		List<Man> oListTwo = new ArrayList<>();
		oList.addAll(oListTwo);
		System.err.println("size two ==" + oList.size());

		oList.add(new Man("abc", 5));
		for (int i = 0; i < oList.size(); i++) {
			Man man = oList.get(i);
			if (man.age == 5) {
				oList.set(i, new Man("bcd", man.age));
			}
		}
		for (Man man : oList) {
			System.out.println(man.toString());
		}

	}


	private static void iteratorList() {
		// 迭代器 遍历
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < 10; i++) {
			list.add("Hello" + i);
		}
		Iterator<String> iterator2 = list.iterator();
		while (iterator2.hasNext()) {
			String next = iterator2.next();
			System.out.println("iterator2 ==" + next);
			if ("Hello3".equals(next)) {
				iterator2.remove();
			}
		}

	}

	private static void addCollection() {
		List<String> varietyCommonPlants = new ArrayList<>();
		TreeSet<String> set = new TreeSet<>();
		set.add("n" + set.size());
		set.add("n" + set.size());
		set.add("n" + set.size());
		set.add("n" + set.size());
		set.add("n" + set.size());
		set.add("n" + set.size());

		varietyCommonPlants.addAll(set);
		for (String string : varietyCommonPlants) {
			System.out.println(string);
		}

	}


	// 插入到倒数第2位置
	private static void addSecondLastPosition() {
		List<Man> oList = new ArrayList<>();
		System.out.println(oList.size() + " size ");
		List<Man> oListTwo = new ArrayList<>();
		oList.addAll(oListTwo);
		System.err.println("size two ==" + oList.size());

		List<String> aList = new ArrayList<>();
		aList.add("sfs");
		aList.add("sfs111");
		for (int i = 0; i < 10; i++) {
			aList.add(aList.size() == 0 ? 0 : aList.size() - 1, i + "");
		}

		System.out.println("+++++++++++++++++");
		for (int i = 0; i < aList.size(); i++) {
			System.out.println(aList.get(i));
		}
	}

	private static void inside() {
		List<Integer> ilList = new ArrayList<>();

		ilList.add(1);
		ilList.add(2);
		ilList.add(3);
		ilList.add(0, 111111);
		ilList.add(ilList.size() - 1, 2222);
		// .add(1,22222);
		System.out.println(ilList.get(ilList.size() - 1));
		System.out.println(ilList.get(0));
	}
}

class Man {

	public Man() {
		super();
	}

	public Man(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	String name;
	int age;

	@Override
	public String toString() {
		return "Man [name=" + name + ", age=" + age + "]";
	}

}
